package com.sanery.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.sanery.dao.DiscoveryDao;
import com.sanery.dao.MyBatisUtil;
import com.sanery.entity.Discovery;
import com.sanery.service.DiscoveryService;

public class DiscoveryServiceImpl implements DiscoveryService {

	SqlSession session = MyBatisUtil.getSession();
	private DiscoveryDao dao = session.getMapper(DiscoveryDao.class);

	@Override
	public List<Discovery> getAll() {
		List<Discovery> list = dao.getAll();
		MyBatisUtil.closeSession(); // 关闭
		return list;
	}

	@Override
	public void add(Discovery discovery) {
		Date date = new Date();
		discovery.setDate(date);
		try {
			dao.addDiscovery(discovery);
			session.commit();// 提交事务
		} catch (Exception e) {
			session.rollback(); // 有异常说明有问题，回滚
			throw new RuntimeException(e);
		} finally {
			MyBatisUtil.closeSession(); // 关闭
		}
	}

	@Override
	public void delDiscovery(Integer id) {
		try {
			dao.delDiscovery(id);
			session.commit();// 提交事务
		} catch (Exception e) {
			session.rollback(); // 有异常说明有问题，回滚
			throw new RuntimeException(e);
		} finally {
			MyBatisUtil.closeSession(); // 关闭
		}
	}

	@Override
	public List<Discovery> getMy(Integer id) {
		List<Discovery> list = dao.getMy(id);
		MyBatisUtil.closeSession(); // 关闭
		return list;
	}

	@Override
	public List<Discovery> getSel(Date begintime, Date endtime, Integer id) {
		List<Discovery> list = dao.getSel(begintime, endtime, id);
		MyBatisUtil.closeSession(); // 关闭
		return list;
	}

}
